﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Sunlight.Dispatch.Dal;

namespace Sunlight.Dispatch.UI {
    public partial class ParameterFirstSettting : UserControl {
        public event EventHandler<EventArgs> Submitted;

        /// <summary>
        /// 初始化该控件的内容
        /// </summary>
        public void Initialize(string connectionString) {
            this.ClearCurrentContent();

            using(var context = new MonitorSettingDataContext(connectionString)) {
                var business = context.TBusiness.ToList();
                var receive = context.TSide.Distinct().ToList();

                this.comboBoxBusiness.DataSource = business;
                this.comboBoxBusiness.DisplayMember = "Name";
                this.comboBoxBusiness.ValueMember = "Code";

                this.comboBoxReceive.DataSource = receive;
                this.comboBoxReceive.DisplayMember = "Name";
                this.comboBoxReceive.ValueMember = "Code";

                //此处必须重新取实例，否则会有级联选择的Bug
                this.comboBoxSend.DataSource = new List<TSide>(receive);
                this.comboBoxSend.DisplayMember = "Name";
                this.comboBoxSend.ValueMember = "Code";
            }
        }

        /// <summary>
        /// 清空界面内容
        /// </summary>
        public void ClearCurrentContent() {
            this.textBoxCode.Text = string.Empty;
            this.textBoxName.Text = string.Empty;
            this.textBoxUpfunction.Text = string.Empty;
            this.textBoxDownfunction.Text = string.Empty;
            this.textBoxMemo.Text = string.Empty;

            this.comboBoxBusiness.DataSource = null;
            this.comboBoxReceive.DataSource = null;
            this.comboBoxSend.DataSource = null;

            this.radioButtonRun.Checked = true;
            this.textBoxThread.Text = "1";
            this.textBoxSysNumber.Text = "1";
            this.radioButtonAsync.Checked = true;
        }

        public ParameterFirstSettting() {
            InitializeComponent();
            this.Load += ParameterFirstSettting_Load;
        }

        private void ParameterFirstSettting_Load(object sender, EventArgs e) {

        }

        public void RaiseSubmitted() {
            if(this.Submitted != null)
                this.Submitted.Invoke(this.groupBoxContainer, null);
        }
    }
}
